import {configureStore} from '@reduxjs/toolkit';
import user from "@/redux/reducer/user";
import role from "@/redux/reducer/role";
import {StatePageType} from "@/redux/index";
import BaseEntity from "@/type/BaseEntity";

// const log = LogFactory.getLogger("redux.store");
// const reducers_ = import.meta.glob("@/redux/reducer/*.tsx", {
//     eager: true,
//     import: "default"
// });
// const reducer_: any = {}
// Object.entries(reducers_)
//     .map(([key, ele]) => {
//         const reducerKey = key.match(new RegExp("[a-z]+.tsx$"));
//         if (reducerKey) {
//             const rk = reducerKey[0].replace(".tsx", "")
//             log.debug("reducerKey", rk)
//             reducer_[rk] = ele;
//         }
//     })

export const store = configureStore({
    reducer: {
        user,
        role
    }
    // middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(logger).concat(thunk)
});

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _state_ = store.getState();

type StateType = typeof _state_
/**
 * store的keys的key的联合类型,约束ProTable组件的storeName属性
 * 此store名字必须是继承StatePageType
 */
export type StoreKeys<E extends BaseEntity> = {
    [K in keyof StateType]: StateType[K] extends StatePageType<E> ? K : never
}[keyof StateType]

// const x: StoreKeys<SecurityUser> = "user";


// Infer the `RootState` and `AppDispatch` types form the store itself
export type RootState = ReturnType<typeof store.getState>;
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
export type AppDispatch = typeof store.dispatch;
export default store;